package pe.edu.upc.xcode.food.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import pe.edu.upc.xcode.food.dao.PlatoDAO;
import pe.edu.upc.xcode.food.domain.Plato;

@Repository
@Transactional
public class PlatoDAOImpl implements PlatoDAO {

	
    @PersistenceContext
    private EntityManager em;
	

	public PlatoDAOImpl() {

	}

	public Integer save(Plato object) {
		em.persist(object) ;		
		return 1;
	}

	public Integer update(Plato object) {
		em.merge(object);
		return 0;
	}

	public Integer detele(Plato object) {
		em.merge(object);
		return 0;
	}

	public Plato find(Plato object) {
		Query query = em.createQuery("SELECT p from Plato p where p.codigo=:parameter");
		query.setParameter("parameter", object.getCodigo());
		Plato p= (Plato) query.getSingleResult();
		return p;
	}

	public List<Plato> findAll() {
		 Query query = em.createQuery("SELECT p from Plato p");
		 List<Plato> list= query.getResultList();
		 return list;
	}

}
